package com.winit.openapi.wh.validator;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.ValidateDateUtitls;
/**
 * 
 * 预约送仓单校验器类
 * 
 * @version 
 * <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年12月3日 	Created
 *
 * </pre>
 * @since 1.
 */
public class ReservationSendWhValidator {
    
    /**
     * 查询待预约单 入参校验
     * @param json
     */
    public static void validateWaittingReservationOrder(JSONObject json) {
        ValidateUtil.validNotNull(json, "source");
        ValidateUtil.validMaxLength(json, "source",10, true);
        
        //客户编码
        ValidateUtil.validNotNull(json, "customerCode");
        ValidateUtil.validMaxLength(json, "customerCode", 30);
        String source=json.getString("source");
        if("P".equals(source))
        {
            //订单号
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
        }else{
            //目的仓编码
            ValidateUtil.validNotNull(json, "destinationWhCode");
            ValidateUtil.validMaxLength(json, "destinationWhCode", 40);
        }
    }
    
    /**
     * 创建预约单入参校验
     * @param json
     */
    public static void validateCreateReservationOrder(JSONObject json) {
        // 送仓时间
        ValidateUtil.validNotNull(json, "sendWhDate");
        Object sendWhDate=json.get("sendWhDate");
        ValidateDateUtitls.validDate(sendWhDate, "sendWhDate");
        // 提前预约时间
//        ValidateUtil.validNotNull(json, "advanceBookingTime");
        ValidateUtil.validInteger(json, "advanceBookingTime", true);
//        ValidateUtil.validDateString(json, "advanceBookingTime", DateUtil.FULL_DATE_STRING);
        // 联系人
        ValidateUtil.validMaxLength(json, "contactPerson", 20,true);
        // 联系电话
        ValidateUtil.validMaxLength(json, "contactPhone", 20,true);
        
        // 订单集合
        ValidateUtil.validNotNull(json, "orderNoList");
        ValidateUtil.validList(json, "orderNoList", true);
        JSONArray orderNoList = json.getJSONArray("orderNoList");
        if (orderNoList.size() == 0) {
           // throw new ApiException(ErrorCode.GOODS_NOT_NULL);
        }
        for (Object obj : orderNoList) {
            JSONObject orderInfo = (JSONObject) obj;
            // 订单号
            ValidateUtil.validMaxLength(orderInfo, "orderNo",64, true);
            //验货仓编码
            ValidateUtil.validMaxLength(orderInfo, "inspectionWhCode", 40,true);
            //验货仓名称
            ValidateUtil.validMaxLength(orderInfo, "inspectionWhName", 50,true);
        }
        // 目的仓编码
        ValidateUtil.validMaxLength(json, "destinationWhCode", 40, true);
        // 目的仓名称
        ValidateUtil.validMaxLength(json, "destinationWhName", 50, true);
        // 箱型集合
        ValidateUtil.validNotNull(json, "packageTypeList");
        ValidateUtil.validList(json, "packageTypeList", true);
        JSONArray packageTypeList = json.getJSONArray("packageTypeList");
        if (packageTypeList.size() == 0) {
           // throw new ApiException(ErrorCode.GOODS_NOT_NULL);
        }
        for (Object obj : packageTypeList) {
            JSONObject  packageType = (JSONObject) obj;
            // 箱型
            ValidateUtil.validMaxLength(packageType, "packageType",50, true);
            //下拉列表值来源
            ValidateUtil.validMaxLength(packageType, "listValueSource",10, true);
            String listValueSource=packageType.getString("listValueSource");
            if("P".equals(listValueSource))
            {
                // 数量
                ValidateUtil.validNotNull(packageType, "quantity");
            }
            ValidateUtil.validMaxLength(packageType, "quantity", 8);
            ValidateUtil.isIntNumber(packageType.getString("quantity"));
        }
    }
    
    /**
     *编辑预约单 入参校验
     * @param json
     */
    public static void validateUpdateReservationOrder(JSONObject json) {
        // 预约单号
        ValidateUtil.validNotNull(json, "reservationOrderNo");
        ValidateUtil.validMaxLength(json, "reservationOrderNo",60, true);
        
        validateCreateReservationOrder(json);
    }
}
